package vcp.client.GUI.mvc.model;

import java.io.Serializable;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import org.joda.time.DateTime;

import vcp.client.interfaces.IObservable;
import vcp.client.request.CustomerCheckInRequest;
import vcp.client.request.CustomerRegularCheckInRequest;
import vcp.client.utility.Common.UserNotification;
import vcp.entity.Subscription;
import vcp.enums.PopUpMessageType;
import vcp.enums.Screen;
import vcp.enums.SiteEnum;
/**
 * 
 * @author VCP team
 *<p>
 *check in model
 *</p>
 *@see UIAbstractModel
 */
public class UICheckInModel extends UIAbstractModel
{
	/** site number **/
	private SiteEnum site;

	public SiteEnum getSite ()
	{
		return this.site;
	}

	public void setSite ( SiteEnum site )
	{
		this.site = site;
	}
	/** subscription instance **/
	private Subscription subscription;

	public Subscription getSubscription ()
	{
		return this.subscription;
	}

	public void setSubscription ( Subscription subscription )
	{
		this.subscription = subscription;
	}

	public UICheckInModel ( IObservable model )
	{
		super(model);
	}
	/**
	 * check if the info insertion are correct
	 * @param userid
	 * @param carid
	 */
	public void verifyCheckInInfo ( Integer userid, int carid )
	{
		CustomerCheckInRequest request = new CustomerCheckInRequest(userid, carid, this.site);
		this.sendRequest(request, true);
	}
	/**
	 * check if regular check in info are correct
	 * @param date
	 * @param email
	 */
	public void verifyRegularCheckInInfo ( DateTime date, String email )
	{
		CustomerRegularCheckInRequest request = new CustomerRegularCheckInRequest(this.subscription, email, date);
		this.sendRequest(request, true);
	}

	/****************************************************************************************************************************
	 *** Observer methods ***
	 ****************************************************************************************************************************/
	@SuppressWarnings ( "unchecked" )
	@Override
	public void update ( Object data )
	{
		if ( data instanceof Boolean )
		{
			this.controllerDelegate.showPopUpMessage(UserNotification.CHECK_IN_WAS_REGISTERED, PopUpMessageType.INFORMATION);
			this.controllerDelegate.loadMVC(Screen.ONSITE);
		}
		else if ( data instanceof String )
		{
			this.controllerDelegate.showPopUpMessage((String) data, PopUpMessageType.INFORMATION);
			this.controllerDelegate.loadMVC(Screen.ONSITE);
		}
		else
		{
			ArrayList <Serializable> serializablesList = (ArrayList <Serializable>) data;
			boolean passToRegular = true;
			Subscription subscription = null;

			if ( serializablesList.size() > 1 )
			{
				String messagePromp = (String) serializablesList.get(0);
				messagePromp = String.format("%s\nWould you like to check-in regular?", messagePromp);

				int result = JOptionPane.showConfirmDialog(null, messagePromp, null, JOptionPane.OK_CANCEL_OPTION);
				passToRegular = result == JOptionPane.OK_OPTION;
				if ( passToRegular )
				{
					subscription = (Subscription) serializablesList.get(1);
				}
			}
			else
			{
				subscription = (Subscription) serializablesList.get(0);
			}

			if ( passToRegular )
			{
				this.controllerDelegate.showPopUpMessage(UserNotification.PASS_TO_REGULAR_CHECK_IN, PopUpMessageType.INFORMATION);
				this.controllerDelegate.loadMVCWithData(Screen.REGULAR_CHECK_IN, subscription);
			}
			else
			{
				this.controllerDelegate.loadingStop();
			}

		}
	}

}
